<!-- src/components/GlobalNotification.vue -->
<template>
  <div class="notification-container">
    <transition-group name="notification" tag="div">
      <div
        v-for="notification in notifications"
        :key="notification.id"
        :class="['notification', notification.type]"
      >
        <div class="notification-content">
          <span class="notification-icon" v-if="notification.type === 'success'">✓</span>
          <span class="notification-icon" v-if="notification.type === 'error'">✗</span>
          <span class="notification-icon" v-if="notification.type === 'warning'">⚠</span>
          <span class="notification-icon" v-if="notification.type === 'info'">ℹ</span>
          <span class="notification-message">{{ notification.message }}</span>
          <button class="notification-close" @click="removeNotification(notification.id)">
            ×
          </button>
        </div>
        <div
          class="notification-progress"
          :style="{
            animationDuration: notification.duration + 'ms',
            backgroundColor: getProgressColor(notification.type)
          }"
        ></div>
      </div>
    </transition-group>
  </div>
</template>

<script>
export default {
  name: 'GlobalNotification',
  data() {
    return {
      notifications: []
    }
  },
  methods: {
    addNotification(message, type = 'info', duration = 3000) {
      const id = Date.now() + Math.random()
      const notification = {
        id,
        message,
        type,
        duration
      }

      this.notifications.push(notification)

      // 自动移除通知
      setTimeout(() => {
        this.removeNotification(id)
      }, duration)
    },

    removeNotification(id) {
      const index = this.notifications.findIndex(n => n.id === id)
      if (index > -1) {
        this.notifications.splice(index, 1)
      }
    },

    getProgressColor(type) {
      const colors = {
        success: '#67c23a',
        error: '#f56c6c',
        warning: '#e6a23c',
        info: '#409eff'
      }
      return colors[type] || colors.info
    }
  }
}
</script>

<style scoped>
.notification-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  width: 380px;
}

.notification {
  margin-bottom: 15px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  animation: slideInRight 0.3s ease;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.notification.success {
  background: linear-gradient(135deg, #f0f9eb 0%, #e1f3d8 100%);
  border-left: 5px solid #67c23a;
}

.notification.error {
  background: linear-gradient(135deg, #fef0f0 0%, #fde2e2 100%);
  border-left: 5px solid #f56c6c;
}

.notification.warning {
  background: linear-gradient(135deg, #fdf6ec 0%, #faecd8 100%);
  border-left: 5px solid #e6a23c;
}

.notification.info {
  background: linear-gradient(135deg, #edf2fc 0%, #dbe7fb 100%);
  border-left: 5px solid #409eff;
}

.notification-content {
  display: flex;
  align-items: flex-start;
  padding: 20px;
  position: relative;
}

.notification-icon {
  margin-right: 12px;
  font-weight: bold;
  font-size: 18px;
  min-width: 20px;
  text-align: center;
}

.notification-message {
  flex: 1;
  font-size: 15px;
  line-height: 1.5;
  color: #2d2d2d; /* 从 #333 改为更深的 #2d2d2d */
  font-weight: 500;
}

/* 为不同类型的通知设置更合适的文字颜色 */
.notification.success .notification-message {
  color: #2d5a27; /* 深绿色，与成功状态匹配 */
}

.notification.error .notification-message {
  color: #8c1d1d; /* 深红色，与错误状态匹配 */
}

.notification.warning .notification-message {
  color: #8c5c0c; /* 深橙色，与警告状态匹配 */
}

.notification.info .notification-message {
  color: #1d4d8c; /* 深蓝色，与信息状态匹配 */
}

.notification-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #6b6b6b; /* 从 #909399 改为更深的 #6b6b6b */
  padding: 0;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 15px;
  border-radius: 50%;
  transition: all 0.2s ease;
}

.notification-close:hover {
  color: #2d2d2d; /* 从 #606266 改为更深的 #2d2d2d */
  background-color: rgba(0, 0, 0, 0.05);
}
.notification-progress {
  height: 4px;
  background-color: rgba(0, 0, 0, 0.1);
  animation: progress linear;
  transform-origin: left;
}

@keyframes slideInRight {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

.notification-enter-active {
  animation: slideInRight 0.3s ease;
}

.notification-leave-active {
  animation: slideInRight 0.3s ease reverse;
}

@keyframes progress {
  from {
    transform: scaleX(1);
  }
  to {
    transform: scaleX(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .notification-container {
    width: calc(100% - 40px);
    top: 10px;
    right: 10px;
  }

  .notification-content {
    padding: 15px;
  }

  .notification-message {
    font-size: 14px;
  }

  .notification-icon {
    font-size: 16px;
    margin-right: 10px;
  }
}
</style>
